<?php
class Catalog_CategoryController extends Areal_Controller_Action
{
    public function indexAction()
    {
        $category_id = $this->getRequest()->getParam('category_id');
        $category = Doctrine::getTable('Catalog_Model_Category')->find($category_id);
        $this->view->assign('categories', $category->getActiveSubcategoryList());
        $page_number = $this->_getParam('page');
        $this->view->assign('products', $category->getActiveProductPaginator($page_number));
    }

    public function adminAction()
    {
        $category_id = $this->getRequest()->getParam('category_id');
        $category = Doctrine::getTable('Catalog_Model_Category')->find($category_id);
        $page_number = $this->_getParam('page');
        $filter = new Catalog_Model_ProductAdminFilter();
        $sort = $this->getRequest()->getParam('sort');
        if ($sort){
            $filter->setSort($sort);
            return $this->_helper->redirector('admin','category','catalog',
                                                  array('category_id'=>$category_id,
                                                        'page'=>$page_number));
        }
        $this->view->assign('categories', $category->getAdminSubcategoryList());
        $this->view->assign('products',
                            $filter->getProductAdminPaginator($category_id, $page_number));
        $this->view->assign('category_id', $category_id);
        Catalog_Model_Category::addCategoriesToNavigation($this->view->navigation(), $category_id);
    }

    public function menuAction()
    {
        $rootCategory = Catalog_Model_Category::getCategoryMenuTree();
        $this->view->assign('rootCategory', $rootCategory);
    }

    public function adminMenuAction()
    {
        $rootCategory = Catalog_Model_Category::getCategoryAdminMenuTree();
        $this->view->assign('rootCategory', $rootCategory);
    }

    public function addAction()
    {
        $parent_id = $this->getRequest()->getParam('parent_id');
        $category= new Catalog_Model_Category();
        if ($this->getRequest()->isPost()){
            if ($category->add($this->getRequest()->getPost(), $parent_id)){
                $message = Zend_Registry::get('Zend_Translate')->translate('Category saved');
                $this->getHelper('flashMessenger')->addMessage($message);
                return $this->_helper->redirector('admin','category','catalog',
                                                  array('category_id'=>$parent_id));
            } else {
                $message = Zend_Registry::get('Zend_Translate')->translate('Category not saved');
                $this->getHelper('flashMessenger')->addMessage($message);
            }
        } 
        $this->view->form = $category->getForm();
        Catalog_Model_Category::addCategoriesToNavigation($this->view->navigation(), $parent_id);
    }

    public function editAction()
    {
        $category_id = $this->getRequest()->getParam('category_id');
        $category = Doctrine::getTable('Catalog_Model_Category')->find($category_id);
        if ($this->getRequest()->isPost()){
            if ($category->edit($this->getRequest()->getPost())){
                $message = Zend_Registry::get('Zend_Translate')->translate('Category saved');
                $this->getHelper('flashMessenger')->addMessage($message);
                $parent = $category->getNode()->getParent();
                return $this->_helper->redirector('admin','category','catalog',
                                                  array('category_id'=>$parent['id']));
            } else {
                $message = Zend_Registry::get('Zend_Translate')->translate('Category not saved');
                $this->getHelper('flashMessenger')->addMessage($message);
            }
        } 
        $this->view->form = $category->getForm();
        Catalog_Model_Category::addCategoriesToNavigation($this->view->navigation(), $category_id);
    }

    public function moveAction()
    {
        $category_id = $this->getRequest()->getParam('category_id');
        $category = Doctrine::getTable('Catalog_Model_Category')->find($category_id);
        if ($this->getRequest()->isPost()){
            if ($category->move($this->getRequest()->getPost())){
                $message = Zend_Registry::get('Zend_Translate')->translate('Category moved');
                $this->getHelper('flashMessenger')->addMessage($message);
                $parent = $category->getNode()->getParent();
                return $this->_helper->redirector('admin','category','catalog',
                                                  array('category_id'=>$parent['id']));
            } else {
                $message = Zend_Registry::get('Zend_Translate')->translate('Category could not be moved');
                $this->getHelper('flashMessenger')->addMessage($message);
            }
        }
        $this->view->form = $category->getMoveForm();
        Catalog_Model_Category::addCategoriesToNavigation($this->view->navigation(), $category_id);
    }

    public function deleteAction()
    {
        $category_id = $this->getRequest()->getParam('category_id');
        $category = Doctrine::getTable('Catalog_Model_Category')->find($category_id);
        $form = new Default_Form_ConfirmForm();
        if ($this->getRequest()->isPost()){
            if ($form->isValid($this->getRequest()->getPost())){
                $parent = $category->getNode()->getParent();
                $category->getNode()->delete();
                $message = Zend_Registry::get('Zend_Translate')->translate('Category deleted');
                $this->getHelper('flashMessenger')->addMessage($message);
                return $this->_helper->redirector('admin','category','catalog',
                                                  array('category_id'=>$parent['id']));
            }
        }
        $this->view->form = $form;
        Catalog_Model_Category::addCategoriesToNavigation($this->view->navigation(), $category_id);
    }
}
